/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.x3.purchasing.operator;

import com.x3.purchasing.account.Account;
import com.x3.purchasing.account.AccountDAO;
import com.x3.purchasing.account.AccountDAOImpl;
import com.x3.purchasing.common.ClassApplicationModule;
import com.x3.purchasing.role.Role;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.zkoss.zk.ui.event.Event;
import org.zkoss.zk.ui.event.EventListener;
import org.zkoss.zul.Listbox;
import org.zkoss.zul.Listcell;
import org.zkoss.zul.Listitem;
import org.zkoss.zul.Messagebox;
import org.zkoss.zul.Textbox;
import org.zkoss.zul.Toolbarbutton;

/**
 *
 * @author kris
 */
public class OperatorWnd extends ClassApplicationModule {

    private OperatorDAO operatorDAO;
    private AccountDAO accountDAO;
    private Operator operatorEdit;
    private Account accountEdit;
    private Textbox txtNama;
    private Textbox txtTelp;
    private Textbox txtEmail;
    private Textbox txtUsername;
    private Textbox txtPassword;
    private Listbox listboxData;

    public void onCreate() throws InterruptedException {
        operatorDAO = new OperatorDAOImpl();
        accountDAO = new AccountDAOImpl();
        listboxData = (Listbox) this.getFellow("listboxData");
        txtNama = (Textbox) this.getFellow("txtNama");
        txtTelp = (Textbox) this.getFellow("txtTelp");
        txtEmail = (Textbox) this.getFellow("txtEmail");
        txtUsername = (Textbox) this.getFellow("txtUsername");
        txtPassword = (Textbox) this.getFellow("txtPassword");
        this.load();
    }

    private void load() throws InterruptedException {
        try {
            List<Operator> datas = operatorDAO.gets();
            listboxData.getItems().clear();
            int index = 1;
            for (final Operator operator : datas) {
                Listitem item = new Listitem();
                item.setValue(operator);
                item.appendChild(new Listcell(index + ""));
                item.appendChild(new Listcell(operator.getNama()));
                item.appendChild(new Listcell(operator.getEmail()));
                item.appendChild(new Listcell(operator.getTelp()));

                Toolbarbutton btnEdit = new Toolbarbutton();
                btnEdit.setId("btnEdit" + index);
                btnEdit.setTooltiptext("Klik untuk mengubah data");
                btnEdit.setImage("/img/edit.png");
                btnEdit.addEventListener("onClick", new EventListener() {

                    public void onEvent(Event event) throws Exception {
                        operatorEdit = operator;
                        accountEdit = operator.getAccount();
                        txtNama.setValue(operator.getNama());
                        txtEmail.setValue(operator.getEmail());
                        txtTelp.setValue(operator.getTelp());
                        txtUsername.setValue(operator.getAccount().getUsername());
                        txtPassword.setValue(operator.getAccount().getPassword());
                        txtNama.setFocus(true);
                    }
                });

                Toolbarbutton btnDelete = new Toolbarbutton();
                btnDelete.setId("btnDelete" + index);
                btnDelete.setTooltiptext("Klik untuk menghapus data");
                btnDelete.setImage("/img/delete.png");
                btnDelete.addEventListener("onClick", new EventListener() {

                    public void onEvent(Event event) throws Exception {
                        delete(operator);
                    }
                });

                Listcell cellAksi = new Listcell();
                cellAksi.appendChild(btnEdit);
                cellAksi.appendChild(btnDelete);

                item.appendChild(cellAksi);
                listboxData.appendChild(item);
                index++;
            }
        } catch (Exception ex) {
            ex.printStackTrace();
            Messagebox.show("Gagal menampilkan data Kualifikasi");
        }
    }

    public void save() throws InterruptedException {
        Role role = new Role();
        role.setId(1);
        Account account = new Account();
        account.setUsername(txtUsername.getText());
        account.setPassword(txtPassword.getText());
        account.setRole(role);

        Operator operator = new Operator();
        operator.setNama(txtNama.getText());
        operator.setEmail(txtEmail.getText());
        operator.setTelp(txtTelp.getText());

        try {
            if (operator.getNama().isEmpty() || operator.getTelp().isEmpty()
                    || operator.getEmail().isEmpty() || account.getUsername().isEmpty() || account.getUsername().isEmpty()) {
                Messagebox.show("Data ada yang kosong");
            } else {
                if (operatorEdit == null) {
                    if (isUserExist(account.getUsername())) {
                        Messagebox.show("User telah terdaftar");
                    } else {
                        accountDAO.insert(account);
                        account = accountDAO.getMaxId();
                        operator.setAccount(account);
                        operatorDAO.insert(operator);
                        Messagebox.show("Data berhasil disimpan");
                        this.load();
                        this.clear();
                    }
                } else {
                    if (!account.getUsername().equals(accountEdit.getUsername()) && isUserExist(account.getUsername())) {
                        Messagebox.show("User telah terdaftar");
                    } else {
                        //Account                       
                        account.setId(accountEdit.getId());
                        accountDAO.update(account);
                        //operator
                        operator.setId(operatorEdit.getId());
                        account = accountDAO.getMaxId();
                        operator.setAccount(account);
                        operatorDAO.update(operator);
                        Messagebox.show("Data berhasil disimpan");
                        this.load();
                        this.clear();
                    }
                }
            }
        } catch (Exception ex) {
            Logger.getLogger(OperatorWnd.class.getName()).log(Level.SEVERE, null, ex);
            Messagebox.show("Gagal menyimpan data");
        }
    }

    public void cancel() {
        this.clear();
    }

    private void delete(Operator Operator) throws InterruptedException {
        try {
            operatorDAO.delete(Operator.getId());
            accountDAO.delete(Operator.getAccount().getId());
            Messagebox.show("Operator sudah dihapus");
            this.clear();
            this.load();
        } catch (Exception ex) {
            Logger.getLogger(OperatorWnd.class.getName()).log(Level.SEVERE, null, ex);
            Messagebox.show("Gagal menghapus operator");
        }
    }

    private void clear() {
        operatorEdit = null;
        accountEdit = null;
        txtNama.setText("");
        txtEmail.setText("");
        txtTelp.setText("");
        txtPassword.setText("");
        txtUsername.setText("");
    }

    private boolean isUserExist(String username) {
        try {
            Account account = accountDAO.getByUsername(username);
            return true;
        } catch (Exception exception) {
            return false;
        }
    }
}
